package com.puman.watchtrade.fragment.home.httphandler;

import org.achartengine.chart.CubicLineChart;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.string;
import android.util.Log;

import com.puman.watchtrade.fragment.home.model.ComRecord;
import com.puman.watchtrade.fragment.home.model.ComRecords;
import com.puman.watchtrade.fragment.home.model.GoodsDetail;
import com.puman.watchtrade.fragment.home.model.GoodsDetails;
import com.puman.watchtrade.fragment.home.model.Varietie;
import com.puman.watchtrade.fragment.home.model.Varieties;
import com.puman.watchtrade.util.Config;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.HttpClent;

public class HomeHttpHandler {
	/*
	 * 今日牌品 列表
	 */
	public DataResult VarietiedList(String status, String pageNumber,
			String typeId) {
		DataResult dataResult = new DataResult();
		Varieties varieties = new Varieties();
		String path = Config.baseUrlStr + Config.varietiesList + "status="
				+ status + "&pageNumber=" + pageNumber + "&type_id=" + typeId;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					Varietie varietie = new Varietie();
					varietie.setId(userObject.getString("id"));
					varietie.setGoodsNo(userObject.getString("goodsNo"));
					varietie.setTitle(userObject.getString("name"));
					varietie.setImgUrl(Config.imgUrl
							+ userObject.getString("pic"));
					varietie.setStartTime(userObject.getString("start_time"));
					varietie.setEndTimeString(userObject.getString("end_time"));
					varietie.setDeal(userObject.getString("deal"));
					varieties.varietiesList.add(varietie);
				}
			}
			dataResult.object = varieties;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataResult;
	}

	/*
	 * 获取结束时间
	 */
	public DataResult endTime(String typeId) {
		DataResult dataResult = new DataResult();
		String path = Config.baseUrlStr + Config.endTimeList + "type_id="
				+ typeId;
		String resultJson = HttpClent.getUrlInfo(path);
//		JSONObject jsonObject = new JSONObject();
//		try {
//			jsonObject.put("flag", true);
//			jsonObject.put("msg", "good");
//			JSONArray SecObject = new JSONArray();
//			SecObject.put("2015-08-29 20:00:00");
//			jsonObject.put("data", SecObject);
//		} catch (JSONException e1) {
//			e1.printStackTrace();
//		}
//		
//		resultJson = jsonObject.toString();
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
//				 JSONArray userObject = result.getJSONArray("data");
				 dataResult.object = result.getString("data");
						 }
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.object = "";
			e.printStackTrace();
		}
		return dataResult;
	}

	/*
	 * 商品详情
	 */
	public DataResult GoodsDetailInfo(String goodsNo) {
		DataResult dataResult = new DataResult();
		GoodsDetails goodsDetails = new GoodsDetails();
		String userNo = "";
		if (GlobalData.userInfo != null) {
			userNo = GlobalData.userInfo.getUserNo();
		}
		String path = Config.baseUrlStr + Config.goodsDetail + "goodsNo="
				+ goodsNo + "&userNo=" + userNo;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				GoodsDetail goodsDetail = new GoodsDetail();
				JSONObject jsonArray = result.getJSONObject("data");
				goodsDetail.setId(jsonArray.getString("id"));
				goodsDetail.setEnd_time(jsonArray.getString("end_time"));
				goodsDetail.setName(jsonArray.getString("name"));
				goodsDetail.setPsychologicalPrice(jsonArray.getString("psychologicalPrice"));
				goodsDetail.setPrice(jsonArray.getString("price"));
				goodsDetail.setMyPrice(jsonArray.getString("myprice"));
				goodsDetail.setStart_time(jsonArray.getString("start_time"));
				goodsDetail.setStatus(jsonArray.getString("status"));
				goodsDetail
						.setVistoerCount(jsonArray.getString("vistoerCount"));
				/*
				 * 顶部图片
				 */
				JSONArray topImg = jsonArray.getJSONArray("imgs1");
				for (int i = 0; i < topImg.length(); i++) {
					goodsDetail.goodsTopImg.add(Config.imgUrl
							+ topImg.get(i).toString());
				}
				/*
				 * 详情图片
				 */
				JSONArray detailImg = jsonArray.getJSONArray("imgs");
				for (int i = 0; i < detailImg.length(); i++) {
					goodsDetail.goodsShowImg.add(Config.imgUrl
							+ detailImg.get(i).toString());
				}
				/*
				 * 商品参数
				 */
				JSONArray goodsInfo = jsonArray.getJSONArray("goodsParm");
				for (int i = 0; i < goodsInfo.length(); i++) {
					goodsDetail.goodsParm.add(goodsInfo.get(i).toString());
				}
				/*
				 * 拍卖纪录
				 */
				JSONObject record = jsonArray.getJSONObject("comRecord");
				goodsDetail.comRecords
						.setComCount(record.getString("comCount"));
				goodsDetail.setJoinCount(goodsDetail.comRecords.getComCount());
				JSONArray recordList = record.getJSONArray("comRecordDetail");
				for (int i = 0; i < recordList.length(); i++) {
					ComRecord comRecord = new ComRecord();
					JSONObject recordInfo = recordList.getJSONObject(i);
					comRecord.setOrder(recordInfo.getString("order"));
					comRecord.setPrice(recordInfo.getString("price"));
					comRecord.setUseNo(recordInfo.getString("user_no"));
					comRecord.setUserName(recordInfo.getString("nick"));
					goodsDetail.comRecords.comRecordList.add(comRecord);
				}
				goodsDetails.goodsDetailList.add(goodsDetail);
				dataResult.object = goodsDetails;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}

	/*
	 * 参与竞拍
	 */
	public DataResult takeIn(String goodNo, String price) {
		DataResult dataResult = new DataResult();
		JSONObject param = new JSONObject();
		try {
			param.put("goodsNo", goodNo);
			param.put("price", price);
			param.put("userNo", GlobalData.userInfo.getUserNo());
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String path = Config.baseUrlStr + Config.takeIn + "jsonParams="
				+ param.toString();
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataResult;
	}

}
